package com.baidu.user.service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;

import com.baidu.pojo.App;
import com.baidu.pojo.Pic;
import com.monliao.util.HttpUtil;

public class HtmlParser {
	
		
			/*	public static void main(String[] args) {
					
					String url="http://mm.10086.cn/android/info/300009480221.html?from=www&fw=267.2###";
					App app = getAppHtml(url);
					Map map = getPicHtml(url);
				}
	*/
	
	
				
			public  static Map getPicHtml(String url) {
				Map map=new HashMap();
				
				try {
					Parser parser=new Parser(url);
					parser.setEncoding("UTF-8");
					TagNameFilter tagNameFilter=new TagNameFilter("title");
					NodeList list = parser.parse(tagNameFilter);
					//应用名称
					String appname = list.elementAt(0).toPlainTextString().split("-")[0];
			
				
					File file=new File("D:/apache-tomcat-7.0.42/app/icon");
					
					if  (!file .exists()  && !file .isDirectory())   {
						 file .mkdir();    
					}
					
					//下载轮播图
					parser.reset();
					HasAttributeFilter attributeFilter_pic1=new HasAttributeFilter("id","mj_tu_1");
					HasAttributeFilter attributeFilter_pic2=new HasAttributeFilter("id","mj_tu_2");
					HasAttributeFilter attributeFilter_pic3=new HasAttributeFilter("id","mj_tu_3");
					
					list = parser.parse(attributeFilter_pic1);
					Pic pic1 = downloadpic(list,file,appname);
					parser.reset();
					list = parser.parse(attributeFilter_pic2);
					Pic pic2=downloadpic(list,file,appname);
					parser.reset();
					list = parser.parse(attributeFilter_pic3);
					Pic pic3=downloadpic(list,file,appname);
					map.put("pic1", pic1);
					map.put("pic2", pic2);
					map.put("pic3", pic3);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return map;
				
				}



			public static Pic downloadpic(NodeList list, File file, String appname) {
				
					
					String picsrc = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
					//System.out.println(picsrc);
					String[] picna = picsrc.split("/");
					String picName=picna[picna.length-1];
					String picurl= file+"/"+picName;
					HttpUtil.httpDownload(picsrc, picurl);
					Pic pic=new Pic(UUID.randomUUID()+"", picurl, null, appname);
					return pic;
					
			}




			public static App  getAppHtml(String url){
			
				
				Map map=new HashMap<String,String>();
				App app=null;
				try {
					Parser parser=new Parser(url);
					parser.setEncoding("UTF-8");
					
					
					TagNameFilter tagNameFilter=new TagNameFilter("title");
					
					NodeList list = parser.parse(tagNameFilter);
					
					//应用名称
					String appname = list.elementAt(0).toPlainTextString().split("-")[0];
					
					
					File file=new File("D:/apache-tomcat-7.0.42/app/icon");
					
					if  (!file .exists()  && !file .isDirectory())   {
						 file .mkdir();    
					}
					

					parser.reset();
					
					HasAttributeFilter attributeFilter_appicon=new HasAttributeFilter("id","appicon");
					
					 list = parser.parse(attributeFilter_appicon);
					 //应用图标
					String appiconsrc = ((TagNode)list.elementAt(0)).getAttribute("src");
					//下载应用图像
					String[] icon = appiconsrc.split("/");
					//下载地址
					String appicon=file+"\\"+icon[icon.length-1];
					HttpUtil.httpDownload(appiconsrc, appicon);
					
					
				parser.reset();
				HasAttributeFilter attributeFilter_info=new HasAttributeFilter("class","mj_info font-f-yh");
				list=parser.parse(attributeFilter_info);	
				int msglength= list.toHtml().split("</li><li>").length;
				//System.out.println(list.toHtml());
				
				for (int i = 1; i <msglength; i++) {
					map.put(i,  list.toHtml().split("</li><li>")[i].split("：")[1]. replaceAll("\\s*", ""));
					//System.out.println(appname+"---------"+map.get(i));
				}
				
				
				//二维码地址
			/*	parser.reset();
				HasAttributeFilter attributeFilter_ew=new HasAttributeFilter("class","mj_ew");
				list=parser.parse(attributeFilter_ew);
				String codeurlsrc = ((TagNode)list.elementAt(0).getFirstChild()).getAttribute("src");
			
				//下载地址
				String codeurl=file+"\\"+UUID.randomUUID()+".png";
				HttpUtil.httpDownload(codeurlsrc, codeurl);*/
				
			
				//价格
				String price= map.get(1).toString();
				//版本
				String version=map.get(2).toString();
				//文件大小
				String filesize=map.get(3).toString();
				//开发者
				String developer = map.get(4).toString();
				developer=developer.substring(developer.indexOf(">")+1,developer.indexOf("<",2));
				//System.out.println(developer);
				//应用类型
				String apptype=map.get(5).toString();
				//更新时间
				String updatetime=map.get(6).toString();
				
				//应用简介
				
				parser.reset();
				HasAttributeFilter attributeFilter_mj_yyjs=new HasAttributeFilter("class","mj_yyjs font-f-yh");
				list=parser.parse(attributeFilter_mj_yyjs);
				//System.out.println(( list.elementAt(0).getChildren()).toHtml());
				String description=( list.elementAt(0).getChildren()).toHtml();
				
				//apk下载地址
				parser.reset();
				HasAttributeFilter attributeFilter_xzdbd=new HasAttributeFilter("class","mj_xzdbd");
				list=parser.parse(attributeFilter_xzdbd);
				String apkurlhref = ((TagNode)list.elementAt(0)).getAttribute("href");
				//System.out.println(apkurl);
				//下载
				String apkurl=file+"/"+appname+"-"+filesize+".apk";
			//	HttpUtil.httpDownload(apkurlhref, apkurl);
				
				String downloadqr=file+"/"+appname+"-"+"logo.jpg";
				MatrixToImageWriter.encode(apkurl, 512, 512, appicon, downloadqr);

				app=new App(UUID.randomUUID().toString(),appname, version, appicon, apkurl, description, filesize, updatetime, developer, apptype, price, null, null, null,downloadqr);
			
			//	System.out.println(app.toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return app;
				
				
			}
}
